#include "StdAfx.h"
#include "TMaterial.h"

#define COPY_LIGHT\
	memcpy(m_pfAmb, data.m_pfAmb, 4*sizeof(float));\
	memcpy(m_pfDif, data.m_pfDif, 4*sizeof(float));\
	memcpy(m_pfSpe, data.m_pfSpe, 4*sizeof(float));\
	memcpy(m_pfEmi, data.m_pfEmi, 4*sizeof(float))

TMaterial::TMaterial(void) :
	m_lID(-1), m_sItem(_T("name")), m_sClass(_T("")), m_fShine(0.5f), m_fTransparent(0.f),
	m_fSizePreview(1000.f), m_iFlag(RMF_LOCK_AMBIENT) 
{
	m_pfAmb[0] = 0.0f;
	m_pfAmb[1] = 0.0f;
	m_pfAmb[2] = 0.0f;
	m_pfAmb[3] = 0.5f;

	m_pfDif[0] = 0.6f;
	m_pfDif[1] = 0.6f;
	m_pfDif[2] = 0.6f;
	m_pfDif[3] = 1.0f;

	m_pfSpe[0] = 1.0f;
	m_pfSpe[1] = 1.0f;
	m_pfSpe[2] = 1.0f;
	m_pfSpe[3] = 0.2f;

	m_pfEmi[0] = 0.0f;
	m_pfEmi[1] = 0.0f;
	m_pfEmi[2] = 0.0f;
	m_pfEmi[3] = 0.0f;
}

TMaterial::TMaterial( const TMaterial& data ) : 
	m_lID(data.m_lID), m_sItem(data.m_sItem), m_sClass(data.m_sClass),
	m_fShine(data.m_fShine), m_fTransparent(data.m_fTransparent),
	m_fSizePreview(data.m_fSizePreview), m_iFlag(data.m_iFlag),
	m_difMap(data.m_difMap), m_speMap(data.m_speMap), m_bumMap(data.m_bumMap)
{
	COPY_LIGHT;
}

TMaterial& TMaterial::operator=( const TMaterial& data )
{
	if (&data != this)
	{
		m_lID = data.m_lID;
		m_sItem = data.m_sItem;
		m_sClass = data.m_sClass;
		COPY_LIGHT;
		m_fShine = data.m_fShine;
		m_fTransparent = data.m_fTransparent;
		m_fSizePreview = data.m_fSizePreview;
		m_iFlag = data.m_iFlag;
		m_difMap = data.m_difMap;
		m_speMap = data.m_speMap;
		m_bumMap = data.m_bumMap;
	}
	return *this;
}

BOOL TMaterial::HasDifMap() const
{
	BOOL bEmpty=m_difMap.m_sMapFile.IsEmpty();
	return !bEmpty;
}

BOOL TMaterial::HasBumMap() const
{
	BOOL bEmpty=m_bumMap.m_sMapFile.IsEmpty();
	return !bEmpty;
}

BOOL TMaterial::HasSpeMap() const
{
	BOOL bEmpty=m_speMap.m_sMapFile.IsEmpty();
	return !bEmpty;
}

void TMaterial::OutFile( CArchive &arch )
{
	arch << m_lID;
	arch << m_sItem;
	arch << m_sClass;
	arch << m_pfAmb[0];
	arch << m_pfAmb[1];
	arch << m_pfAmb[2];
	arch << m_pfAmb[3];
	arch << m_pfDif[0];
	arch << m_pfDif[1];
	arch << m_pfDif[2];
	arch << m_pfDif[3];
	arch << m_pfSpe[0];
	arch << m_pfSpe[1];
	arch << m_pfSpe[2];
	arch << m_pfSpe[3];
	arch << m_pfEmi[0];
	arch << m_pfEmi[1];
	arch << m_pfEmi[2];
	arch << m_pfEmi[3];
	arch << m_fShine;
	arch << m_fTransparent;
	arch << m_fSizePreview;
	m_difMap.OutFile(arch);
	m_speMap.OutFile(arch);
	m_bumMap.OutFile(arch);
}

void TMaterial::InFile( CArchive &arch )
{
	arch >> m_lID;
	arch >> m_sItem;
	arch >> m_sClass;
	arch >> m_pfAmb[0];
	arch >> m_pfAmb[1];
	arch >> m_pfAmb[2];
	arch >> m_pfAmb[3];
	arch >> m_pfDif[0];
	arch >> m_pfDif[1];
	arch >> m_pfDif[2];
	arch >> m_pfDif[3];
	arch >> m_pfSpe[0];
	arch >> m_pfSpe[1];
	arch >> m_pfSpe[2];
	arch >> m_pfSpe[3];
	arch >> m_pfEmi[0];
	arch >> m_pfEmi[1];
	arch >> m_pfEmi[2];
	arch >> m_pfEmi[3];
	arch >> m_fShine;
	arch >> m_fTransparent;
	arch >> m_fSizePreview;
	m_difMap.InFile(arch);
	m_speMap.InFile(arch);
	m_bumMap.InFile(arch);
}

void TMaterial::Reset()
{
	*this = TMaterial();
}
TTextureOption::TTextureOption(): m_sMapFile(_T("")), m_fMapRatio(1.f), m_fSizeMapU(200.f), 
		m_fSizeMapV(200.f), m_fOffMapU(0.f), m_fOffMapV(0.f), m_iMapFlag(RMF_MAP_AUTOAXIS) { }

TTextureOption::TTextureOption( const TTextureOption &data ): m_sMapFile(data.m_sMapFile), 
	m_fMapRatio(data.m_fMapRatio), m_fSizeMapU(data.m_fSizeMapU), m_fSizeMapV(data.m_fSizeMapV),
	m_fOffMapU(data.m_fOffMapU), m_fOffMapV(data.m_fOffMapV), m_iMapFlag(data.m_iMapFlag) { }

TTextureOption& TTextureOption::operator=( const TTextureOption& data )
{
	if (&data != this)
	{
		m_sMapFile = data.m_sMapFile;
		m_fMapRatio = data.m_fMapRatio;
		m_fSizeMapU = data.m_fSizeMapU;
		m_fSizeMapV = data.m_fSizeMapV;
		m_fOffMapU = data.m_fOffMapU;
		m_fOffMapV = data.m_fOffMapV;
		m_iMapFlag = data.m_iMapFlag;
	}
	return *this;
}

void TTextureOption::OutFile( CArchive &arch )
{
	arch << m_sMapFile;
	arch << m_fMapRatio;
	arch << m_fSizeMapU;
	arch << m_fSizeMapV;
	arch << m_fOffMapU;
	arch << m_fOffMapV;
	arch << m_iMapFlag;
}

void TTextureOption::InFile( CArchive &arch )
{
	arch >> m_sMapFile;
	arch >> m_fMapRatio;
	arch >> m_fSizeMapU;
	arch >> m_fSizeMapV;
	arch >> m_fOffMapU;
	arch >> m_fOffMapV;
	arch >> m_iMapFlag;
}
int TPixelFormatMask::ComparePixelMask( const TPixelFormatMask *p1, const TPixelFormatMask *p2 )
{
	int bColorBit=0;
	if(p1->cColorBits!=p2->cColorBits) 
	{
		int c1=32-p1->cColorBits;
		int c2=32-p2->cColorBits;
		if(c1>0 && c2>0) 
			bColorBit= (c1<c2? -1 : 1);
		else if(c1<0 && c2<0) 
			bColorBit= (abs(c1)<abs(c2)? -1 : 1);
		else 
			bColorBit= (c1>0? -1 : 1);
	}
	if(bColorBit!=0)
		return bColorBit;

	int bAccumBit=0;
	if(p1->cAccumBits!=p2->cAccumBits) 
	{
		int c1=64-p1->cAccumBits;
		int c2=64-p2->cAccumBits;
		if(c1>0 && c2>0)
			bColorBit= (c1<c2? -1 : 1);
		else if(c1<0 && c2<0) 
			bColorBit= (abs(c1)<abs(c2)? -1 : 1);
		else 
			bColorBit= (c1>0? -1 : 1);
	}
	if(!bAccumBit)
		return bAccumBit;

	int bDepthBit=0;
	if(p1->cDepthBits!=p2->cDepthBits) 
	{
		int c1=32-p1->cDepthBits;
		int c2=32-p2->cDepthBits;
		if(c1>0 && c2>0) 
			bColorBit= (c1<c2? -1 : 1);
		else if(c1<0 && c2<0) 
			bColorBit= (abs(c1)<abs(c2)? -1 : 1);
		else 
			bColorBit= (c1>0? -1 : 1);
	}

	return bDepthBit;	
}